/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hanserver.action.autocomplete;

import br.com.sales.hanserver.dao.Dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author v
 */
public class AutocompleteAction extends Dao {

    public AutocompleteAction() {
    }

    public Set<String[]> getDataByKeyWord(String tableName,
            String attributeNameDesc, String attributeNameID,
            String keyWord, String restriction, String parameter) throws SQLException {
        Set<String[]> setResult = new LinkedHashSet<>();
        
        String query
                = " select " + attributeNameDesc + " DisplayName," + attributeNameID + " Value"
                + " from " + tableName
                + " where  " + attributeNameDesc + " like('%" + keyWord + "%') ";
        if (restriction != null && !"null".equals(restriction)
                && parameter != null && !"null".equals(parameter)) {
            query += restriction.replace("?", parameter);
        }
        query += " order by DisplayName DESC"
                + " LIMIT 10 ";
        try {
            Statement st = getConn().createStatement();
            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                setResult.add(new String[]{rs.getString("DisplayName"), rs.getString("Value")});
            }
        } catch (Exception e) {
            Logger logger = Logger.getLogger(AutocompleteAction.class.getSimpleName());
            logger.log(Level.INFO, "Error search data", e);
        } finally {
            closeConn();
        }
        return setResult;
    }
}
